package com.hxy.controller;

import com.github.pagehelper.PageInfo;
import com.hxy.common.JsonResult;
import com.hxy.pojo.vo.RprecordVo;
import com.hxy.service.RprecordService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.sql.Date;

/**
 * 奖惩记录
 */
@RestController
@RequestMapping("/rprecord")
public class RprecordController {
    @Autowired
    private RprecordService rprecordService;

    //显示奖惩列表
    @RequestMapping("/list")
    public JsonResult show(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                           @RequestParam(value = "limit", required = false, defaultValue = "10") int limit) {
        page = page == null ? 1 : page;
        PageInfo<RprecordVo> rprecordPageInfo = rprecordService.selectAll(page, limit);
        return new JsonResult(0, "查询成功", rprecordPageInfo);
    }

    //    删除当前ID 的奖惩记录
    @RequestMapping("/deleteById")
    public JsonResult delete(@RequestParam(value = "id") Integer id) {
        int i = rprecordService.deleteByPrimaryKey(id);
        if (i > 0) {
            return new JsonResult(0, "删除成功", null);
        }
        return new JsonResult(1, "删除失败", null);
    }

    //    新增一条奖惩记录
    @RequestMapping("/addRprecord")
    public JsonResult addrprecord(@Param(value = "eid") Integer eid,
                                  @Param(value = "rpkindid") Integer rpkindid) {
        System.out.println(eid + "------------ " + rpkindid);
        int i = rprecordService.addRprecord(eid, rpkindid);
        if (i > 0) {
            return new JsonResult(0, "添加成功", null);
        }
        return new JsonResult(1, "添加失败", null);


    }

    //    更新奖惩记录
    @RequestMapping("/update")
    public JsonResult update(RprecordVo Rprecord) {
        int update = rprecordService.update(Rprecord);
        if (update > 0) {
            return new JsonResult(0, "修改成功", null);
        }
        return new JsonResult(1, "修改失败", null);
    }
//    模糊查询
    @RequestMapping("/findRprecordByTJ")
    public JsonResult findRprecordByTJ(@RequestParam(value = "page", required = false, defaultValue = "1") Integer page,
                                       @RequestParam(value = "limit", required = false, defaultValue = "10") int limit,
                                       @RequestParam(value = "eid", required = false, defaultValue = "") Integer eid,
                                       @RequestParam(value = "name", required = false, defaultValue = "") String stuffname,
                                       @RequestParam(value = "starTime", required = false, defaultValue = "") String starTime,
                                       @RequestParam(value = "endTime", required = false, defaultValue = "") String endTime,
                                       @RequestParam(value = "rpKind", required = false, defaultValue = "") String rpking,
                                       @RequestParam(value = "status", required = false, defaultValue = "") Integer status) {
        page = page == null ? 1 : page;
        PageInfo<RprecordVo> trainnotePageInfo = rprecordService.selectByTj(page, limit, eid, stuffname, starTime, endTime, rpking, status);
        if (trainnotePageInfo != null) {
            return new JsonResult(0, "成功", trainnotePageInfo);
        } else {
            return new JsonResult(1, "失败", null);
        }


    }
    //显示奖惩列表
    @RequestMapping("/findById")
    public JsonResult findById(Integer id){
        RprecordVo rprecordVo = rprecordService.selectById(id);
        if (rprecordVo !=null){
            return new JsonResult(0, "成功", rprecordVo);
        }
        return new JsonResult(1, "失败", null);
    }


}

